package com.belf.console.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.belf.common.config.MigrateConfig;
import com.belf.common.core.domain.AjaxResult;
import com.belf.console.agent.migrate.job.JobManageTask;
import com.belf.console.domain.DataTaskConf;
import com.belf.console.domain.DataTaskLog;
import com.belf.console.service.IDataTaskConfService;
import com.belf.console.service.IDataTaskJobManageService;
import com.belf.console.service.IDataTaskLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.belf.common.constant.BelfCodeConstants.BELF_TASK_JOB_STATUS_INIT;

/**
 * IDataTaskManageServiceImpl
 *
 * @author l
 * @since 2024/8/9 14:17
 */
@Service
public class IDataTaskJobManageServiceImpl implements IDataTaskJobManageService {

    private static final Logger logger = Logger.getLogger(IDataTaskJobManageServiceImpl.class.getName());

    @Autowired
    private MigrateConfig migrateConfig;
    @Autowired
    private IDataTaskConfService dataTaskConfService;
    @Autowired
    private IDataTaskLogService dataTaskLogService;


    @Override
    public AjaxResult taskList() {
        List<DataTaskConf> migrateTaskList = getMigrateTaskList();
        return AjaxResult.success(migrateTaskList);
    }

    @Override
    public List<DataTaskConf> getMigrateTaskList() {
        JSONObject paramJson = new JSONObject();
        JSONObject resultJson = JobManageTask.manageJob(migrateConfig.jobList(), paramJson);
        List<DataTaskConf> results = new ArrayList<>();

        if (resultJson != null) {
//            List<DataTaskConf> data1 = resultJson.getList("data", DataTaskConf.class);
            JSONArray jsonArray =resultJson.getJSONArray("data");
            jsonArray.forEach(data ->{
                JSONObject jsonObject = JSONObject.parseObject(data.toString());
                Long jobId = jsonObject.getLong("job_id");
                Long taskId = jsonObject.getLong("task_id");
                DataTaskConf dataTaskConf = dataTaskConfService.selectDataTaskConfById(taskId);
                dataTaskConf.setJobId(jobId);
                results.add(dataTaskConf);
            });
        }
        return results;
    }

    @Override
    public AjaxResult executeTaskById(Long id) {
        return null;
    }
    @Override
    public AjaxResult taskStop(Long id) {
        logger.log(Level.INFO, "taskStop");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("job_id",id);
        jsonObject = JobManageTask.manageJob(migrateConfig.jobStop(), jsonObject);
        DataTaskLog dataTaskLog = dataTaskLogService.selectDataTaskLogById(id);
        DataTaskConf dataTaskConf = new DataTaskConf();
        dataTaskConf.setId(dataTaskLog.getTaskId());
        dataTaskConf.setTaskStatus(BELF_TASK_JOB_STATUS_INIT);
        dataTaskConfService.updateDataInfo(dataTaskConf);
        return AjaxResult.success(jsonObject);
    }


}
